package com.luckymore.ydd.app.util;

import com.base.control.MActivitySupport;
import com.base.model.BackgroundResource;
import com.base.util.BackgroundSetter;
import com.base.util.ConstantResource;
import com.base.util.DateUtil;
import com.base.util.DeviceUuidFactory;
import com.base.util.StringUtil;
import com.luckymore.ydd.app.R;
import com.luckymore.ydd.app.asyncTask.UpdateMQuestion;
import com.luckymore.ydd.app.cache.CacheUtil;
import com.luckymore.ydd.app.db.DataBaseUtil;
import com.luckymore.ydd.app.db.MQuestionDaoActivity;
import com.luckymore.ydd.app.funcs.login.LoginActivity;
import com.luckymore.ydd.app.model.BCourseList;
import com.luckymore.ydd.app.model.MAppQuestion;
import com.luckymore.ydd.app.model.MAppUser;
import com.luckymore.ydd.app.model.TrackByDay;
import com.luckymore.ydd.app.model.UserPreference;
import com.luckymore.ydd.app.net.ApiClientFactory;
import com.luckymore.ydd.app.net.DefaultRequestCallback;

import java.util.Arrays;

public class AppConfig {
    public static boolean isFirst = false;
    public static boolean isNewUserFirstDay = false;
    public static boolean isDifferentAccount = false; //是否切换了不同的账号
    public static boolean isCreateByWeb = false; //是否是服务器自动创建的账号

    public static void checkIsNewUser(MAppUser.MUser mUser) {
        if(DateUtil.getCurrentTime("yyyy-MM-dd").equals(mUser.getCreateTime())) {
            AppConfig.isNewUserFirstDay = true;
        }
    }

    public static final String APK_NAME = "ydd.apk";

    //当前版本号
    public static final int VERSION = 1;

    //默认首页预下载天数
    public static final int INDEX_PRE_DAY = 11;

    //相机返回保存路径
    public static final String TEMP_FILE_NAME = "temp" + AppConfig.PIC_FORMAT;

    public static final String HEAD_IMG_FILE_NAME = "headImg" + AppConfig.PIC_FORMAT;
    public static String HEAD_IMG_NAME = ConstantResource.USERID + "headImg" + AppConfig.PIC_FORMAT;

    //as复习最少个数
    public final static int NOTE_REVIEW_MIN_COUNT = 30;

    public final static String DEFAULT_DIARY_HINT = "用了研大大，考研不用怕，感觉棒棒哒！";

    //照片格式
    public final static String PIC_FORMAT = ".jpg";

    //单例课程列表
    private static BCourseList courseList;

    //用户首选项， 单例
    private static UserPreference preference;

    //数据库对应类
    public static Class<?> DATA_CLASS[] = {MAppQuestion.MQuestion.class, TrackByDay.class};
    public static Class<? extends MActivitySupport> LOGIN_ACTIVITY_CLASS = LoginActivity.class;

    public final static String MD5_KEY = "";

    public static String PASSWORD = "";

    //框架有关的配置项
    public final static String APPID = "ydd";

    //Cache
	public final static String TEMP_PATH =  APPID + "_temp"; //缓存目录
    public final static Integer CACHE_DAY = 300; //缓存300天

    //请求地址
    public static String URL_PRO = "http://114.215.196.179:8080/gs/"; //114.215.196.179
    public static String URL = URL_PRO + "mobile";
    public static String DOWNLOAD_URL = URL_PRO + "download.do";
    public static String UPLOAD_URL = URL_PRO + "fileUpload.do";

    public static String signShareUrl = "http://s2.smartjiangsu.com/web/gs/sign.html?userid=%s";

	//view control 手势翻页距离
	public final static int FLIP_DISTANCE = 50;

    public static void initApplication(MQuestionDaoActivity context) {
        ConstantResource.deviceUuidFactory = new DeviceUuidFactory(context);
        ConstantResource.TEMP_PATH = AppConfig.TEMP_PATH;
        ConstantResource.URL = URL;
        ConstantResource.DOWNLOAD_URL = DOWNLOAD_URL;
        ConstantResource.UPLOAD_URL = UPLOAD_URL;
        ConstantResource.CACHE_DAY = CACHE_DAY;
        ConstantResource.APPID = APPID;
        ConstantResource.MD5_KEY = MD5_KEY;
        ConstantResource.DATA_CLASS = DATA_CLASS;
        ConstantResource.LOGIN_ACTIVITY_CLASS = LOGIN_ACTIVITY_CLASS;
        ConstantResource.APK_NAME = APK_NAME;


        TrackByDay track = DataBaseUtil.getTrackForBackground(context, DateUtil.getCurrentTime("yyyy-MM-dd"));
        if(track == null) {
            track = TrackByDay.createNewTrack();
        }

        //添加自定义background和src设置
        BackgroundSetter.addBackground(Arrays.asList( //0 背景； 1 ImageView
                new BackgroundResource(R.id.welcome_layout, R.drawable.welcome_bg, 0),
                new BackgroundResource(R.id.login_layout, R.drawable.login_background, 0),
                new BackgroundResource(R.id.functions_bg, R.drawable.today_background_blur, 2, StringUtil.emptyIfBlank(track.getImgGn3()), true),
                new BackgroundResource(R.id.index_bg, R.drawable.today_background_normal, 2, StringUtil.emptyIfBlank(track.getImg()), true),
                new BackgroundResource(R.id.login_logo, R.drawable.login_logo, 1),
                new BackgroundResource(R.id.note_bg_img, R.drawable.note_bg, 2, StringUtil.emptyIfBlank(track.getImgGn()), true),
                new BackgroundResource(R.id.note_today_rec, R.drawable.note_today_rec, 1),
                new BackgroundResource(R.id.note_my_footprint, R.drawable.note_footprint, 1),
                new BackgroundResource(R.id.note_print_img, R.drawable.printer, 1),
                new BackgroundResource(R.id.backup_circle1, R.drawable.backup_circle1, 1),
                new BackgroundResource(R.id.backup_circle2, R.drawable.backup_circle1, 1),
                new BackgroundResource(R.id.welcome_play_video, R.drawable.welcome_play, 1),
                new BackgroundResource(R.id.welcome_begin, R.drawable.welcome_begin, 1),
                new BackgroundResource(R.id.note_head_img_bg, R.drawable.head_bg_rotate, 1)
                ));
//        ConstantResource.setImageLoader(context);

        ConstantResource.fileHelper.init(context);
    }

    //切换账号，清除一些暂时性状态
    public static void changeAccount(MActivitySupport activitySupport) {
        AppConfig.isFirst = false;
        AppConfig.isNewUserFirstDay = false;
        isDifferentAccount = false;
        isCreateByWeb = false;
        UpdateMQuestion.endRunning();
        ApiClientFactory.updateCourseInfo(activitySupport, new DefaultRequestCallback(), null, AppConfig.getCourseList(activitySupport));
    }

    public static synchronized void updateVerify(String verify, String userid) {
        ConstantResource.VERIFY = verify;
        ConstantResource.USERID = userid;
    }

    public static BCourseList getCourseList(MActivitySupport context) {
        if(courseList == null) {
            courseList = CacheUtil.getCourseInfo(context);
        }
        return courseList;
    }

    public static void setCourseList(BCourseList courseList, MActivitySupport context) {
        AppConfig.courseList = courseList;
        if(courseList != null)
            courseList.setUpdateVersion(courseList.getUpdateVersion() + 1);
        CacheUtil.updateCourseInfo(context, courseList);
    }

    public static UserPreference getPreference(MActivitySupport context) {
        if(preference == null) {
            preference = CacheUtil.getUserPreference(context);
            if(preference == null)
                preference = new UserPreference();
        }

        return preference;
    }

    public static void setPreference(UserPreference preference, MActivitySupport context) {
        AppConfig.preference = preference;
        CacheUtil.updateUserPreference(context, preference);
    }

    public static void addSignCount(MActivitySupport activitySupport) {
        UserPreference preference1 = getPreference(activitySupport);
        preference1.setSignCount(preference1.getSignCount() + 1);
        setPreference(preference1, activitySupport);
    }
}
